<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
	public function __construct()
    {
        parent::__construct();
		$this->load->helper('url');
		$this->load->helper('elizer');
		$this->load->library('session');
		$this->load->helper(array('form', 'url'));
		$this->lang->load('english', 'english');
		if($this->session->userdata('logged_in')){
			if($this->session->userdata('user_type') != 0){
				redirect('index');
			}
		}else{
			redirect('login');
		}
    }
	public function index()
	{
		$this->load->library('session');
		$this->load->helper(array('url'));
		$this->load->model('Usermodel');
		$this->load->model('Usermodel');
		$data['title'] = "Admin";
		$data['adminInfor'] = $this->Usermodel->getAdminInfo();
		
		$allClient = $this->Usermodel->getAllClient();
		if($allClient == null){
			$clientList = null;
		}else{
			$clientList = array();
			$index=0;
			foreach ($allClient as $client){
				if($client->is_wife == 0){
					$clientList[$index]['husband'] = $client;
					foreach ($allClient as $tempClient){
						if($client->partner_id == $tempClient->id){
							$clientList[$index]['wife'] = $tempClient;
							break;
						}
					}
					$index++;
				}
			}
		}	
		
		$data['clientList'] = $clientList;
		$this->load->view('admin/admin', $data);
	}
	public function detail($clientId = ''){
		$this->load->helper(array('url'));
		$this->load->model('Usermodel');	
		$data['title'] = "Admin";
		$data['adminInfor'] = $this->Usermodel->getAdminInfo();
		$data['title'] =  "Title";
		
		if(!$clientId){
			redirect('admin');
		}
		$husband = $this->Usermodel->getUserById($clientId);
		if(!$husband || $husband->is_wife == 1){
			redirect('admin');
		}
		
		$wife = $this->Usermodel->getUserById($husband->partner_id);
		if(!$wife){
			redirect('admin');
		}
		$this->load->model('UserDataModel');
		
		$data['title'] = "Client's detail";
		
		$data['husband'] = $husband;
		$data['wife'] = $wife;
	
		$data['husbandBefore'] = $this->UserDataModel->getUserData($husband->id, 0);
		$data['husbandPresent'] = $this->UserDataModel->getUserData($husband->id, 1);
		$data['husbandTest3'] = $this->UserDataModel->getUserData($husband->id, 2);
		
		$data['wifeBefore'] = $this->UserDataModel->getUserData($wife->id, 0);
		$data['wifePresent'] = $this->UserDataModel->getUserData($wife->id, 1);
		$data['wifeTest3'] = $this->UserDataModel->getUserData($wife->id, 2);
		
		$this->load->view('admin/detail', $data);
	}
	public function create(){
		$this->load->library('form_validation');
		$this->load->helper(array('url'));
		$this->load->model('Usermodel');
		
		$data['adminInfor'] = $this->Usermodel->getAdminInfo();
		$data['title'] =  "New client";
		
		$this->form_validation->set_rules('hus-firstname', 'Husband\'s First Name', 'trim|required');
		$this->form_validation->set_rules('hus-lastname', 'Husband\'s Last Name', 'trim|required');
		$this->form_validation->set_rules('hus-username', 'Husband\'s Username', 'trim|required|min_length[6]|alpha_numeric');
		$this->form_validation->set_rules('hus-email', 'Husband\'s Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('hus-password', 'Husband\'s Password', 'trim|required|min_length[6]|alpha_numeric');
		
		$this->form_validation->set_rules('wife-firstname', 'Wife\'s First Name', 'trim|required');
		$this->form_validation->set_rules('wife-lastname', 'Wife\'s Last Name', 'trim|required');
		$this->form_validation->set_rules('wife-username', 'Wife\'s Username', 'trim|required|min_length[6]|alpha_numeric');
		$this->form_validation->set_rules('wife-email', 'Wife\'s Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('wife-password', 'Wife\'s Password', 'trim|required|min_length[6]|alpha_numeric');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('admin/createclient', $data);
		}
		else
		{
			$husFirstName=trim($this->input->post('hus-firstname'));
			$husLastName=trim($this->input->post('hus-lastname'));
			$husUserName=trim($this->input->post('hus-username'));
			$husEmail=trim($this->input->post('hus-email'));
			$husPassword=trim($this->input->post('hus-password'));
			
			$wifeFirstName=trim($this->input->post('wife-firstname'));
			$wifeLastName=trim($this->input->post('wife-lastname'));
			$wifeUserName=trim($this->input->post('wife-username'));
			$wifeEmail=trim($this->input->post('wife-email'));
			$wifePassword=trim($this->input->post('wife-password'));
			
			$errorArray = array();
			
			if($this->Usermodel->checkUsernameExisted($husUserName)){
				$errorArray[] = "Husband's username was existed";
			}
			if($this->Usermodel->checkEmailExisted($husEmail)){
				$errorArray[] = "Husband's email was existed";
			}
			if($this->Usermodel->checkUsernameExisted($wifeUserName)){
				$errorArray[] = "Wife's username was existed";
			}
			if($this->Usermodel->checkEmailExisted($wifeEmail)){
				$errorArray[] = "Wife's email was existed";
			}
			
			if(count($errorArray)){
				$data['errorArray'] = $errorArray;
				$this->load->view('admin/createclient', $data);
			}else{
			
				$husInforArray = array($husUserName, md5($husPassword),$husEmail, "", $husFirstName, $husLastName, 0, 0, 1);
				
				$husId = $this->Usermodel->insertUser($husInforArray);
				if($husId){
					$wifeInforArray = array($wifeUserName, md5($wifePassword),$wifeEmail,"", $wifeFirstName, $wifeLastName, $husId, 1, 1);
					$wifeId = $this->Usermodel->insertUser($wifeInforArray);
					$husUpdateInforArray = array(md5($husPassword),$husEmail, "", $husFirstName, $husLastName, $wifeId, 0, 1, $husId);			
					$this->Usermodel->updateUser($husUpdateInforArray);
					
					$this->load->library('email');
					$this->email->from('info@e-lizer.com', 'E-lizer');
					//Send email to husband
					$this->email->to($husEmail);
										
					$this->email->subject('E-lizer\'s notification');
					$this->email->message('<html>
												<body>
												<p>Dear Mr. '.$husFirstName.' '.$husLastName.'</p>
												<p>Your account in E-lizer has been created:</p>
												<p>Username: '.$husUserName.'</p>
												<p>Password: '.$husPassword.'</p>
												<p>Please go to <a href=\"'.base_url().'\">website</a> to do the test.</p>
												</body>
											</html>');
					
					$this->email->send();
					
					//Send email to wife
					$this->email->to($wifeEmail);
					$this->email->subject('E-lizer\'s notification');
					$this->email->message('<html>
												<body>
												<p>Dear Mrs. '.$wifeFirstName.' '.$wifeLastName.'</p>
												<p>Your account in E-lizer has been created:</p>
												<p>Username: '.$wifeUserName.'</p>
												<p>Password: '.$wifePassword.'</p>
												<p>Please go to <a href=\"'.base_url().'\">website</a> to do the test.</p>
												</body>
											</html>');
					
					$this->email->send();
				}
				redirect('admin');
			}
		}
	}
	public function profile(){
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->model('Usermodel');
		$adminInfor = $this->Usermodel->getAdminInfo();
		$data['adminInfor'] = $adminInfor;
		$data['title'] = "Profile";
		
		$this->form_validation->set_rules('first-name', 'First name', 'trim|required');
		$this->form_validation->set_rules('last-name', 'Last name', 'trim|required');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('new-password', 'New Password', 'trim|min_length[6]|alpha_numericmatches[confirm-password]');
		$this->form_validation->set_rules('confirm-password', 'Confirm Password', 'trim|min_length[6]|alpha_numeric');
		
		if ($this->form_validation->run() == FALSE){
			if($this->input->post('submit')){
				$data['validate'] = true;
			}else{
				$data['validate'] = false;
			}
			$this->load->view('admin/profile', $data);
		}else{
			$title = trim($this->input->post('title'));
			$firstName=trim($this->input->post('first-name'));
			$lastName=trim($this->input->post('last-name'));
			$email=trim($this->input->post('email'));
			$newPassword=trim($this->input->post('new-password'));
			$confirmPassword=trim($this->input->post('confirm-password'));
			$drNote = trim($this->input->post('dr-note'));
			
			if((strlen($newPassword) > 0 || strlen($confirmPassword) > 0) &&  $newPassword!=  $confirmPassword){
				$errorArray = array();
				$errorArray[] = "Password does not match";
				$data['errorArray'] = $errorArray;
			}else{
				if(strlen($newPassword) > 0 && strlen($confirmPassword) > 0){
					$password = md5($newPassword);
				}else{
					$password = $adminInfor->password;
				}
				$adminInforArray = array($email, $password, $email, $title, $firstName, $lastName, 0, 0, 0, $drNote, $adminInfor->id);
				if($this->Usermodel->updateAdminUser($adminInforArray)){
					$errorArray = array();
					$errorArray[] = "Updated successfully";
					$data['errorArray'] = $errorArray;
				}
			}
			$data['drNote'] = $drNote;
			$this->load->view('admin/profile', $data);
		}

	}
}